//
//  FRPPhotoViewController.m
//  FunctionalReactivePixels
//
//  Created by Linyongzhi on 2017/4/10.
//
//

#import "FRPPhotoViewController.h"

#import "FRPPhotoModel.h"
#import "FRPPhotoImporter.h"

#import <SVProgressHUD.h>

@interface FRPPhotoViewController ()

@property (assign, nonatomic) NSInteger photoIndex;
@property (strong, nonatomic) FRPPhotoModel *photoModel;

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation FRPPhotoViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil photoModel:(FRPPhotoModel*)photoModel index:(NSInteger)photoIndex {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (!self) {
        return nil;
    }
    
    self.photoIndex = photoIndex;
    self.photoModel = photoModel;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    RAC(self, imageView.image) = [RACObserve(self.photoModel, fullsizedData) map:^id _Nullable(id  _Nullable value) {
        
        if ([SVProgressHUD isVisible]) {
            [SVProgressHUD dismiss];
        }
        
        return [UIImage imageWithData:value];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [SVProgressHUD show];
    
    [FRPPhotoImporter downloadFullsizedImageForModel:self.photoModel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
